package com.dst.smart.dao.impl.factory;

import java.util.ArrayList;
import java.util.List;

import com.dst.smart.app.MyApplication;
import com.dst.smart.model.SubscribeInfo;
import com.dst.smart.model.SubscribeInfo;
import com.dst.smart.service.SendCMD;
import com.dst.smart.tools.AppTools;

public class SubscribeFactory {
	public static List<SubscribeInfo> list = new ArrayList<SubscribeInfo>();

	public static List<SubscribeInfo> getAll() {
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase()
					.getSubScribe();
		}
		return list;
	}

	public static List<SubscribeInfo> getSubscribeInfo(String voiceName,
			String pinying) {
		List<SubscribeInfo> resultList = new ArrayList<SubscribeInfo>();
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase()
					.getSubScribe();
		}

		for (SubscribeInfo info : list) {
			if (info.getSubscribeName().indexOf(voiceName) != -1
					|| voiceName.indexOf(info.getSubscribeName()) != -1) {
				resultList.add(info);
			} else {
				String pinyingDev = SendCMD.getPinYin(info.getSubscribeName());
				if ((!pinyingDev.equals("") && (pinyingDev.length() > 0))) {
					if (pinying.indexOf(pinyingDev) != -1) {
						resultList.add(info);
					}
				}
			}
		}
		return resultList;
	}

	public static int isExite(String voiceName, String voiceAnswer) {
		int number = 0;
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase()
					.getSubScribe();
		}
		for (SubscribeInfo info : list) {
			if (info.getSubscribeName().equals(voiceName)
					&& info.getSubscribeEvent().equals(voiceAnswer)) {
				number = 1;
				break;
			}
		}
		return number;
	}

	public static int isExite(int Id) {
		int number = 0;
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase()
					.getSubScribe();
		}
		for (SubscribeInfo info : list) {
			if (info.getId() == Id) {
				number = 1;
				break;
			}
		}
		return number;
	}

	public static SubscribeInfo getSubscrite(String _date, int week, String time) {
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase()
					.getSubScribe();
		}
		SubscribeInfo info = null;
		for (SubscribeInfo _info : list) {
			String date = _info.getSubscribeDate();
			if (AppTools.isNumeric(date)) {
				if ((Integer.parseInt(date) & (1 << week)) > 0) {
					if ( _info.getSubscribeTime().equals(time)) {
						info = _info;
						break;
					}
				}
			} else {
				if (date.equals(_date)
						&& (_info.getSubscribeTime()).equals(time)) {
					info = _info;
					break;
				}
			}

		}
		return info;
	}

	public static void clearList() {
		list = MyApplication.getInstance().getWidgetDataBase().getSubScribe();
	}
}
